<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
defined ('IN_AGORA') or die;

class TaskController extends AgoraPostController {
	function _default() {
	
		$topic_id = (int)Agora::getVar('id');
		if (!(int)$topic_id) {
			Agora::showMessage(Agora::lang('Bad request'));
			Agora::redirect(Agora::getRefferer());
		}
		$topic = $this->topic_model->load($this->id);
		$this->authenticate($topic['forum_id'],'post_reply');
		if ( strpos($_SERVER['PHP_SELF'],'index2.php')!=0 ) $mode='agroup';
		else $mode='';

		$forum_model = & Model::getInstance('ForumModel');
		$forum = $forum_model->load($topic['forum_id']);

		if (!$topic) {
			Agora::show404();
			return;
		}
		
        // create pagination with respect to current page
        $per_page = intval($this->agora_user['disp_posts']) > 0 ? $this->agora_user['disp_posts'] : intval($this->agora_config['o_disp_posts_default']);
        $num_pages = ceil(($topic['num_replies'] + 1) / $per_page);
        $this->setPagination($num_pages);
        $page = Agora::getPage($num_pages);
		
		$message = Agora::getSessionVar('message');
		
        Agora::setSessionVar('message', '');
		$catnav_helper = & $this->helper('catnav');
		$catnav_helper->fromForum($topic['forum_id']);

		$parser = & $this->helper('parser');
		$posts = $this->topic_model->loadPosts($topic_id, $per_page, $page,intval($this->agora_user['reverse_posts']), $topic['num_replies']+1);
		$post_helper = &$this->helper('post');
		$posts = $post_helper->processPosts($posts, $parser);
		
		$access_helper = & $this->helper('access');
		$access = $access_helper->getForumAccess($topic['forum_id']);

		$this->view->assign('access',$access);
        $this->view->assign('topic',$topic);
        $this->view->assign('topic_id',$topic_id);
		$this->view->assign('posts',$posts);
		$this->view->assign('forum',$forum);
        $this->view->assign('page',$page);
		
		$this->view->assign('post_body',$message);
		
			
			$topic_username = Agora::getPostVar('topic_username','');
			$topic_username = empty($topic_username) ? $this->agora_user['username'] : $topic_username;
			$topic_email = Agora::getPostVar('guest_email','');
			$topic_email = empty($topic_email) ? false : $topic_email;
			$this->view->assign('topic_username',AString::escape($topic_username));
			$this->view->assign('topic_email',AString::escape($topic_email));
		
		if ($this->agora_user['is_guest']) {
			$banned = false;
		} else {
			$ban_model = & Model::getInstance('BanModel');
			$banned = $ban_model->isBanned($this->agora_user['username']);
		}
		$this->view->assign('user_banned',$banned);
		
		if ('agroup' == $mode) {
			$this->view->template = 'post/reply_agroup';
		} else {
			$this->view->template = 'post/reply';
		}
		$this->view->assign('topic_id',intval($this->id));
	}

	function preview() {
		$this->_preview();
		$this->display();
		jexit(0);
	}

	function _preview() {
		$topic = $this->topic_model->load($this->id);
		$this->authenticate($topic['forum_id'],'post_reply');

		$forum_model = & Model::getInstance('ForumModel');
		$forum = $forum_model->load($topic['forum_id']);

/*		if (!$this->agora_user['is_superadmin'] &&
			$this->access_model->authenticate($this->agora_user['id'],$forum['id'],'use_captcha')) {
			$captcha = Agora::getPostVar('captcha');
			$c_key = Agora::getSessionVar('captcha');

			if (trim($captcha) !== trim($c_key)) {
				Agora::showMessage('Bad captcha');
				Agora::redirect(Agora::getRefferer());
			}
			$post['captcha'] = $captcha;
		}*/

		$raw_message = Agora::getPostVar('req_message','');
		$message = $raw_message;
		$hide_smilies = Agora::getPostVar('hide_smilies',0);

		$parser = & $this->helper('parser');

		$message = $parser->parseMessage($message,$hide_smilies);

		$signature = '';
		if ($this->agora_user['show_sig']) {
			$signature = $parser->parseSignature($this->agora_user['signature']);
		}

		$post = array();
		$post['user'] = $this->agora_user;
		//Agora::prepareUser($post['user'],$this->agora_config['o_ranks']);
		$user_helper = & $this->helper('user');
		$post['user'] = $user_helper->prepareUserAvatar($post['user']);
		$post['user'] = $user_helper->prepareUserTitle($post['user']);

		$post['id'] = 1;
		$post['poster_id'] = $this->agora_user['id'];
		$post['poster'] = $this->agora_user['username'];
		$post['posted'] = time();

		$post['message'] = & $message;
		$post['user']['signature'] = & $signature;

		$this->view->assign('post',$post);
		$this->view->assign('access',array());
		$this->view->assign('post_num','-');
		$this->view->assign('topic_id',$this->id);

		$this->view->assign('raw_message',$raw_message);
		$this->view->assign('hide_smilies',$hide_smilies);

/*			$this->view->assign('merge',$merge);
			$this->view->assign('subscribe',$subscribe);*/

		$this->view->template = 'post/preview';
	}

	function _save() {
	
		if (Agora::getPostVar('preview')) {
			$this->_preview();
			return;
		}

		$topic = $this->topic_model->load($this->id);
		$this->authenticate($topic['forum_id'],'post_reply');

		$forum_model = & Model::getInstance('ForumModel');
		$forum = $forum_model->load($topic['forum_id']);
		$approved = ($this->agora_user['is_superadmin'] || $this->agora_user['is_admin'] || $this->agora_user['is_moderator']) ? 1 : !(int)$forum['use_moderation'];

		if (!$this->agora_user['is_superadmin'] &&
			$this->access_model->authenticate($this->agora_user['id'],$forum['id'],'use_captcha')) {
			$captcha = Agora::getPostVar('captcha');
			$c_key = Agora::getSessionVar('captcha');

			if (trim($captcha) !== trim($c_key) || !$captcha) {
			    Agora::setSessionVar('message', Agora::getPostVar('req_message','', true));
				Agora::showMessage(Agora::lang('Bad captcha'));
				Agora::redirect(Agora::getRefferer());
			}
		}

		$message = Agora::getPostVar('req_message','', true);
		$parser = & $this->helper('parser');
		$message = $parser->fix_broken_bbcode($message);
		$merge = intval(Agora::getPostVar('merge',0));
		$subscribe = Agora::getPostVar('subscribe',0);
		$hide_smilies = Agora::getPostVar('hide_smilies',0);
		$topic_username = Agora::getPostVar('topic_username','');
		$topic_username = empty($topic_username) ? $this->agora_user['username'] : $topic_username;
		$topic_email = Agora::getPostVar('guest_email','');
		$topic_email = empty($topic_email) ? false : $topic_email;
		
		if ($approved) {
		
			// Trigger onBeforeReply Agora plugin event
			JPluginHelper::importPlugin('agora');
			$dispatcher =& JDispatcher::getInstance();
			$options = new JObject;
			$options->forum_id = $topic['forum_id'] ;
			$options->subject = $topic['subject'] ;
			$options->user_name = $topic_username;
			$options->user_id = $this->agora_user['id'];
			$options->user_email = $topic_email;
			$options->message = $message ;
			$options->topic_id = $this->id;
			$plg_result = $dispatcher->trigger('onBeforeReply', array(&$options));
			
		}
		
		// Add reply to the topic
		$post_id = $this->topic_model->addPost($this->id, $topic['forum_id'], $message, $this->agora_user['id'], $topic_username, $hide_smilies, $approved, $topic_email);

		if ($approved) {
		
			// Trigger onAfterReply Agora plugin event
			$options->post_id = $post_id;
			$plg_result = $dispatcher->trigger('onAfterReply', array(&$options)) ;	
			
			// If AUP plugin exists - add user points
			$api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
			if (file_exists($api_AUP))	{
				require_once ($api_AUP);
				AlphaUserPointsHelper::newpoints( 'plgaup_reply_agora', '', '', Agora::lang('AUP_REPLY') );
			}

			// Send notification to subscribed users
			$subscription_helper = &$this->helper('subscription');
			$subscription_helper->newPost($post_id, $topic['forum_id']);
			
		} else {
			Agora::showMessage(Agora::lang('POST_MODERATION_MESSAGE'));
		}
		
		// Subscribe user to this topic
		if ($subscribe) {
			$subscription_model = & Model::getInstance('SubscriptionModel');
			$user_id = $this->agora_user['id'];
			if (!$subscription_model->isSubscribed($user_id, $topic['id'])) {
				$subscription_model->add(array('user_id' => $user_id,'topic_id' => $topic['id']));
				Agora::showMessage(Agora::lang('You are now subscribed on topic ') . ' ' . AString::escape($topic['subject']));
			} else {
				Agora::showMessage(Agora::lang('You are already subscribed on topic ') . ' '  . AString::escape($topic['subject']));
			}

		}
			
		// Send notification to mods and admins if the post is moderated
		if (!$approved) {
			$subscription_helper = &$this->helper('subscription');
			$subscription_helper->newPost($post_id, $topic['forum_id'], true);
		}

		// Build redirect url
		$url = array('task'	=>'topic', 'id'	=> $this->id);
		if (!$this->agora_user['reverse_posts']) {
			$posts = $topic['num_replies'];
			$per_page = intval($this->agora_user['disp_posts']) > 0 ? $this->agora_user['disp_posts'] : intval($this->agora_config['o_disp_posts_default']);
			$num_pages = ceil(($posts+2) / $per_page);
			if ($num_pages > 1) {
				$url['p'] = $num_pages;
			}
		}		
		
		// Redirect
        if (strpos($_SERVER['PHP_SELF'],'index2.php') != 0) {
			Agora::redirect(Agora::makeURL($url,true,true). "#p$post_id");
		} else {
			Agora::redirect(Agora::makeURL($url). "#p$post_id");
		}
	}

	function quote() {
		$post_model = & Model::getInstance('PostModel');
		$post = $post_model->load($this->id);
		$this->id = $post['topic_id'];
		
		$user_model = & Model::getInstance('UserModel');
		$user_fullname = $user_model->getUserFullName($post['poster']);
		
		$username = $this->agora_config['o_fullnames_enabled'] ? $user_fullname : $post['poster'];
		$username = str_replace(array('[',']'), array('&amp;#91;','&amp;#93;'), $username);
		
		Agora::setSessionVar('message', '[quote=' . $username . ']' . $post['message'] . '[/quote]');
		
		$this->_default();
	}
}
?>
